﻿Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.VersionControl.Client

Public Class TfsConnection
    Inherits MarshalByRefObject

    Public vcs As VersionControlServer
    Public MyCred As System.Net.NetworkCredential
    Public server As TeamFoundationServer

    ''' <summary>
    ''' Default constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        MyBase.New()

    End Sub

    ''' <summary>
    ''' Constructor who invoke to ConncetToServer method
    ''' </summary>
    ''' <param name="Login"></param>
    ''' <param name="Password"></param>
    ''' <param name="IpPortOrMachineName"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal Login As String, ByVal Password As String, ByVal IpPortOrMachineName As String)
        MyBase.New()
        Me.ConnnectToServer(Login, Password, IpPortOrMachineName)
    End Sub

    ''' <summary>
    ''' Create credentials and conncet to TFS server with them
    ''' </summary>
    ''' <param name="Login"></param>
    ''' <param name="Password"></param>
    ''' <param name="IpPortOrMachineName"></param>
    ''' <remarks></remarks>
    Public Sub ConnnectToServer(ByVal Login As String, ByVal Password As String, ByVal IpPortOrMachineName As String)

        Try

            MyCred = New System.Net.NetworkCredential(Login, Password)
            server = New TeamFoundationServer(IpPortOrMachineName, MyCred)
            vcs = CType(server.GetService(GetType(VersionControlServer)), VersionControlServer)

        Catch ex As Exception

            Throw New Exception("Unable to connect. Details:" & vbCrLf & ex.Message.ToString)

        End Try

    End Sub

    ''' <summary>
    ''' Useful when works in local and remote mode to difference betwen local or remote instances by computer name
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetComputerName() As String
        Return My.Computer.Name
    End Function

End Class
